package com.fei.config;

import com.fei.bean.UserBean;
import com.fei.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;

public class UserRealm extends AuthorizingRealm {

    @Autowired
    UserService userService;

    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    //权限
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        UsernamePasswordToken userToken = (UsernamePasswordToken) token;
        UserBean userBean = userService.loginUserShiroService(userToken.getUsername());

        //没有这个人
        if(userBean == null){
            return null;
        }

        //存入session
        Subject cureentSubject = SecurityUtils.getSubject();
        Session session = cureentSubject.getSession();
        session.setAttribute("user",userBean);

        return new SimpleAuthenticationInfo(userBean,userBean.getPassword(),"");
    }

}
